package com.kehutong.department.service;

import com.kehutong.common.DoveClient;
import com.kehutong.department.entity.ThirdCommunity;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author zhouweipeng
 * @version 1.0
 * @date 2021/9/9 14:20
 */
@WebService("/basic/thridCommunity/service")
public class ThridCommunityService {
    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private DoveClient doveClient;
    private static final Logger logger = LoggerFactory.getLogger(ThridCommunityService.class);

    /**
     * 保存员工与小区的关系
     * @param jsonObject {"companyNo":"111","list":[{"communityId":"1","employeeIds":["111","222"]},{"communityId":"2","employeeIds":["111","222"]}]}
     * @return
     */
    @ReqMapping("/batchThridCommunityRelation")
    public Object batchThridCommunityRelation(JSONObject jsonObject){
        try {
            String companyNo=jsonObject.getString("companyNo");  //公司编号
            JSONArray jsonArray=jsonObject.getJSONArray("list");  //关系列表
            for(int i=0,len=jsonArray.size();i<len;i++){
                JSONObject relation=jsonArray.getJSONObject(i);
                String communityId=relation.getString("communityId");
                ThirdCommunity thirdCommunity=jdbcSession.findOne(ThirdCommunity.class)
                        .eq("thirdId",communityId).eq("companyNo",companyNo).exe();
                if(thirdCommunity!=null){
                    JSONArray employeeIdArray=relation.getJSONArray("employeeIds");
                    List<String>employeeIds=new ArrayList<>();
                    for(int j=0;j<employeeIdArray.size();j++){
                        employeeIds.add(employeeIdArray.get(i).toString());
                    }
                    thirdCommunity.setEmployeeIds(employeeIds);
                    jdbcSession.updateById(thirdCommunity);
                }
            }
        } catch (Exception e) {
            logger.error("第三方公司批量同步员工与小区的关系出现异常{}",e);
        }
        return Result.success();
    }

    /**
     *  {"companyNo":"111","opType":"1","list":[{"communityId":"1","communityName":"龙门"},{"communityId":"2","communityName":"测试小区"}]}
     * @param jsonObject
     * @return
     */
    @ReqMapping("/batchThridCommunity")
    public Object batchThridCommunity(JSONObject jsonObject){
        try {
            JSONObject param=new JSONObject();
            JSONArray communityList=new JSONArray();
            String companyNo=jsonObject.getString("companyNo");  //公司编号
            String opType=jsonObject.getString("opType"); //操作类型 1.增、2.修改、3.删除
            JSONArray jsonArray=jsonObject.getJSONArray("list");  //小区列表
            for(int i=0,len=jsonArray.size();i<len;i++){
                JSONObject communityObj=jsonArray.getJSONObject(i);
                JSONObject temp=new JSONObject();
                String communityId=communityObj.getString("communityId");  //第三方的id
                String communityName=communityObj.getString("communityName");  //小区名称
                if("1".equals(opType)){
                    ThirdCommunity thirdCommunity=jdbcSession.findOne(ThirdCommunity.class)
                            .eq("thirdId",communityId).eq("companyNo",companyNo).exe();
                    if(thirdCommunity!=null){
                        continue;
                    }
                }
                temp.put("communityId",communityId);
                temp.put("communityName",communityName);
                communityList.add(temp);
            }
            param.put("opType",opType);
            param.put("communityList",communityList);
            // TODO: 2021/9/9 远端调用接口做批量的小区增删改操作
            JSONObject result = doveClient.post("/basic/community/service/saveThridBatch", (http) -> {
                http.setBody(param.toJSONString());
            });
            if (result.getIntValue("code") == 200) {
                JSONArray relationArray=result.getJSONArray("data");
                for(int j=0,len=relationArray.size();j<len;j++){
                    JSONObject relation=relationArray.getJSONObject(j);
                    ThirdCommunity thirdCommunity= Pool.newInstance(ThirdCommunity.class);
                    thirdCommunity.setCompanyNo(companyNo);
                    thirdCommunity.setThirdId(relation.getString("thirdId"));
                    thirdCommunity.setKhtId(relation.getString("khtId"));
                    jdbcSession.insert(thirdCommunity);
                }
            }
        } catch (Exception e) {
            logger.error("第三方公司批量同步小区出现异常{}",e);
        }
        return Result.success();
    }

}
